# -*- coding: UTF-8 -*-

import lda_model
import web_apis
import mashups
import similar_word_model
from gensim.corpora import Dictionary
import CF_recommend
import random
import math

class Evaluate:
    def __init__(self):
        # self.sim_word_model = similar_word_model.Similar_Word_Model()
        self.mashups = mashups.Mashups(use_w2v=False, sim_word_model=None)
        self.apis = web_apis.Web_APIs(self.mashups.api_dict)
        temp = [i for i in range (self.mashups.mashup_count)]
        self.test_set = random.sample(temp, math.floor(len(temp) / 10.0))
        self.train_set = list(set(temp).difference(self.test_set))
        print('get train set done, size = ', len(self.train_set))
        sentences = []
        for i in self.train_set:
            sentences.append(self.mashups.sentences[i])
        self.bow_dict = Dictionary(sentences)
        self.corpus_bow = [self.bow_dict.doc2bow(sentence) for sentence in sentences]
        self.topic_model = lda_model.Topic_Model('lda_with_w2v.model', docs=self.mashups.raw_descs, corpus=self.corpus_bow, id2word=self.bow_dict, num_topics=225)
        print('get topic model done')
        print(self.topic_model.model.print_topics())

    def test(self):
        tick = 0
        rec = 0.0
        prec = 0.0
        for i in self.test_set:
            tick += 1
            if tick % 100 == 0:
                print('test tick:', tick)
            desc = self.mashups.raw_descs[i]
            true_related_apis = self.mashups.api_list[i]
            true_set = set(true_related_apis)
            d = CF_recommend.get_topn_similar_api(desc, self.apis, self.mashups, self.topic_model, self.train_set, topn=10)
            # print(true_set, d)
            r = 0.0
            for k in d:
                if k in true_set:
                    r += 1.0
            rec += r / len(true_related_apis)
            prec += r / 10
        rec /= len(self.test_set)
        prec /= len(self.test_set)
        print('recall:', rec)
        print('precision:', prec)
        print('f-score:', 2.0 * rec * prec / (rec + prec))

                   
if __name__ == '__main__':
    eval = Evaluate()
    eval.test()